package com.youease.chris.view.mediator
{
	import com.youease.chris.control.CreateTextureCommand;
	import com.youease.chris.manager.GlobalObject;
	import com.youease.chris.manager.TexturesManager;
	import com.youease.chris.model.vo.TextureData;
	import com.youease.chris.view.TextureList;
	import com.youease.chris.view.TexturePanel;
	import com.youease.chris.view.TextureRenderer;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.utils.Dictionary;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.List;
	import mx.core.Application;
	import mx.core.ClassFactory;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class TexturePanelMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "TextureMediator";
		
		public function TexturePanelMediator(viewComponent:TexturePanel)
		{
			super(NAME, viewComponent);
			texturePanel.deleteTexture.addEventListener(MouseEvent.CLICK,onDeleteTexture);
//			this.texturePanel.textures.setStyle("headerHeight",40)
		}
		
		public function onDeleteTexture(evt:MouseEvent):void {
			if(GlobalObject.currentTexture)
				Alert.show("确定要删除   "+GlobalObject.currentTexture.img+"  吗？","",Alert.YES|Alert.NO,Sprite(Application.application),onDeleteConfirm);
			else
				Alert.show("请选择要删除的素材");
		}
		
		private function onDeleteConfirm(evt:CloseEvent):void {
			if(evt.detail == Alert.YES) {
				var selectedTexture:TextureData = GlobalObject.currentTexture;
				if(selectedTexture) {
					delete TexturesManager.textures[selectedTexture.img];
					var file:File = new File(GlobalObject.projectDir+"/textures/"+selectedTexture.img);
					file.deleteFile();
					this.sendNotification(CreateTextureCommand.CREATE_TEXTURE_COMPLETE);
				}
			}
		}
		
		public function onSelectTexture(evt:ListEvent):void {
			var currentList:TextureList = evt.currentTarget as TextureList;
			GlobalObject.currentTexture = currentList.selectedItem as TextureData;
		}
		
		override public function listNotificationInterests():Array {
			return [
				CreateTextureCommand.CREATE_TEXTURE_COMPLETE
			];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case CreateTextureCommand.CREATE_TEXTURE_COMPLETE:
//					var arr:ArrayCollection = new ArrayCollection();
//					for each(var td:TextureData in TexturesManager.textures) {
//						if(td.big == 0)
//						arr.addItem(td);
//					}
//					arr.source.sortOn("img", Array.NUMERIC );
//					texturePanel.textures.dataProvider = arr;
					
					this.texturePanel.textures.removeAllChildren();
					
					var dic:Dictionary = new Dictionary();
					for each(var td:TextureData in TexturesManager.textures) {
						var imgName:String = td.img.substring(0,td.img.lastIndexOf("."));
						td.type = GlobalObject.texturesType[imgName.substring(0,imgName.length-2)];
						td.index = int(imgName.substring(imgName.length-2));
					
						if(td.big == 0) {
							if(!dic[td.type]) {
								dic[td.type] = new ArrayCollection();
							}
							dic[td.type].addItem(td);
						}
					}
					
					var i:int = -1;
					for each(var coll:ArrayCollection in dic) {
						var vbox:VBox = new VBox();
						BindingUtils.bindProperty(vbox,"width",this.texturePanel.textures,"width");
						BindingUtils.bindProperty(vbox,"height",this.texturePanel.textures,"height");
						vbox.label = GlobalObject.texturesName[coll.getItemAt(0).type.toString()];
						var list:TextureList = new TextureList();
						list.dataProvider = coll;
						vbox.addChild(list);
						this.texturePanel.textures.addChild(vbox);
						list.addEventListener(ListEvent.ITEM_CLICK,onSelectTexture);
						i++;
					}
					
					for(;i>-1;i--) {
						this.texturePanel.textures.getHeaderAt(i).setStyle("fontSize",12);
						this.texturePanel.textures.getHeaderAt(i).setStyle("fontWeight","normal");
					}
					
					break;
			}
		}
			
		public function get texturePanel():TexturePanel {
			return this.viewComponent as TexturePanel;
		}
	}
}